<template>
  <div>
    <el-table
      :data="paginatedTableData"
      :height="height"
      :max-height="maxHeight"
      :stripe="stripe"
      :border="border"
      :fit="fit"
      :show-header="showHeader"
      :highlight-current-row="highlightCurrentRow"
      :row-class-name="rowClassName"
      :header-cell-class-name="headerCellClassName"
    >
      <el-table-column
        v-for="col in columns"
        :key="col.key"
        :prop="col.prop"
        :label="col.label"
        :width="col.width"
        :min-width="col.minWidth"
        :resizable="col.resizable"
        :sortable="col.sortable"
        :sort-method="col.sortMethod"
        :class-name="col.className"
        :label-class-name="col.labelClassName"
        :show-overflow-tooltip="col.showOverflowTooltip"
      >
        <template #default="scope" v-if="col.custom">
          <!-- 表格中添加插槽 -->
          <slot :name="col.prop" v-bind="scope"></slot>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="mt-20px"
      layout="prev, pager, next,sizes"
      v-if="pagination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[10, 20, 30, 40]"
      :total="total"
      v-model:page-size="pageSize"
      v-model:current-page="currentPage"
      background
    ></el-pagination>
  </div>
</template>

<script setup lang="ts">
import { defineEmits, withDefaults, toRefs, watch, watchEffect } from 'vue'
interface Column {
  key?: string
  prop?: string
  label: string
  width?: number
  minWidth?: number
  resizable?: boolean
  sortable?: boolean
  sortMethod?: (a: any, b: any) => number
  formatter?: (row: Record<string, any>, column: Column, cellValue: any) => any
  className?: string
  labelClassName?: string
  showOverflowTooltip?: boolean
  custom?: boolean
}
interface Props {
  columns: Column[]
  data: Record<string, any>[]
  pagination?: boolean
  pageSize?: number
  height?: number | undefined
  maxHeight?: number | undefined
  stripe?: boolean
  border?: boolean
  fit?: boolean
  showHeader?: boolean
  highlightCurrentRow?: boolean
  rowClassName?: ((row: Record<string, any>) => string) | undefined
  headerCellClassName?: any
}

const props = withDefaults(defineProps<Props>(), {
  columns: () => [],
  data: () => [],
  pagination: false,
  pageSize: 10,
  height: undefined as number | undefined,
  maxHeight: undefined as number | undefined,
  stripe: false,
  border: false,
  fit: true,
  showHeader: true,
  highlightCurrentRow: false,
  rowClassName: undefined as ((row: Record<string, any>) => string) | undefined,
  headerCellClassName: undefined as ((column: Column) => string) | undefined
})
const state = reactive({
  tableData: [...props.data],
  total: 0,
  currentPage: 1,
  paginationEnabled: props.pagination as boolean,
  pageSize: props.pageSize as number
})
watchEffect(() => {
  if (!state.paginationEnabled) {
    state.currentPage = 1
  }
  state.total = state.tableData.length
})

function handleSizeChange(pageSize: number) {
  state.pageSize = pageSize
  emit('pagination-change', { page: state.currentPage, pageSize })
}

function handleCurrentChange(currentPage: number) {
  state.currentPage = currentPage
  emit('pagination-change', { page: currentPage, pageSize: state.pageSize })
}

const paginatedTableData = computed(() => {
  if (!state.paginationEnabled) {
    return state.tableData
  }
  const start = (state.currentPage - 1) * state.pageSize
  const end = start + state.pageSize
  return state.tableData.slice(start, end)
})

const tableData = toRefs(state).tableData
const total = toRefs(state).total
const currentPage = toRefs(state).currentPage
const paginationEnabled = toRefs(state).paginationEnabled
const pageSize = toRefs(state).pageSize

watch([tableData, paginationEnabled], () => {
  updatePagination()
})

function updatePagination() {
  if (paginationEnabled.value) {
    const pageCount = Math.ceil(tableData.value.length / pageSize.value)
    if (currentPage.value > pageCount && pageCount > 0) {
      currentPage.value = pageCount
    }
    total.value = tableData.value.length
  } else {
    currentPage.value = 1
    total.value = tableData.value.length
  }
}
watch(
  () => props.data,
  (val: Record<string, any>[]) => {
    state.tableData = val
  },
  { deep: true }
)
const emit = defineEmits(['pagination-change'])
</script>
